﻿/*------------------------------------------------------------------------

CRITICAL.HPP v3.0.1

Copyright 楊志賢 CxxlMan, 2008 - 2011
All Rights Reserved


------------------------------------------------------------------------*/


#if !defined(__CRITICAL_HPP_CxxlMan)
#define __CRITICAL_HPP_CxxlMan

#include "DLLDEF.HPP"

#if defined(_CXXLCOMMON_DLLEXPORT)
#define CXXLCOMMON_DLLEXPORT CXXL_DLLEXPORT
#else
#define CXXLCOMMON_DLLEXPORT CXXL_DLLIMPORT
#endif

namespace CxxlMan
{

class CXXLCOMMON_DLLEXPORT CriticalSec;

// 關鍵區域的介面
class CXXLCOMMON_DLLEXPORT ICriticalSec
{
  virtual void cxxlFASTCALL Enter(void) = 0;        // 進入關鍵區域
  virtual void cxxlFASTCALL Leave(void) = 0;        // 離開關鍵區域

public:
  ICriticalSec(void){}
  virtual ~ICriticalSec(void){}

  friend class CXXLCOMMON_DLLEXPORT CriticalSec;
};

// 關鍵區域的類別
class CXXLCOMMON_DLLEXPORT CriticalSec
{
  ICriticalSec *Imp;
public:
  CriticalSec(void);       // Constructor
  ~CriticalSec(void);      // Destructor
  void cxxlFASTCALL Enter(void) const        // 進入關鍵區域
  {
    Imp->Enter();
  }
  void cxxlFASTCALL Leave(void) const        // 離開關鍵區域
  {
    Imp->Leave();
  }
};


// 關鍵區域物件專用的物件前置後置處理類別
class CriticalSecHandle
{
  const CriticalSec &Handle;
public:
  CriticalSecHandle(const CriticalSec &CS)  // Constructor
    :Handle(CS)
  {
    CS.Enter();
  }
  ~CriticalSecHandle(void) // Destructor
  {
    Handle.Leave();
  }
};

}   /* namespace CxxlMan */
#endif
